package componenti.diagramDesigner;

import java.awt.Dimension;
import java.util.Collection;
import java.util.LinkedList;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractDataset;
import org.jfree.ui.RefineryUtilities;

import componenti.buffer.Entity;
import componenti.statistica.Esponenziale;
import componenti.statistica.GeneratoreCasuale;

public class TempoAttesaInCoda extends JFrame implements DiagramDesigner {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DefaultCategoryDataset dataset;
	private JFreeChart jfc;
	private double inizio;
	private double fine;

	public TempoAttesaInCoda(String title, Double inizio, Double fine) {
		super(title);
		dataset = new DefaultCategoryDataset();
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.inizio=inizio;
		this.fine=fine;
	}

	@Override
	public void design(Collection<Entity> simulazione) {
		//TODO chiedere a Stefano Belleto'o
		dataset.addValue(0, "Tempo", "" + 0);
		for (Entity entity : simulazione) {
			if (entity.getID() <= inizio && entity.getID() >= fine)
				dataset.addValue(
						entity.getIstanteServizio() - entity.getIstanteArrivo(),
						"Tempo", ((Integer) entity.getID()).toString());
		}
		jfc = ChartFactory.createBarChart("Tempi di attesa in coda", "Utenti",
				"Tempi", dataset, PlotOrientation.VERTICAL, true, true, false);
		ChartPanel chartPanel = new ChartPanel(jfc, false);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(chartPanel);
	}

	@Override
	public AbstractDataset getDataset() {
		return dataset;
	}

	@Override
	public void visualizza() {
		pack();
		RefineryUtilities.centerFrameOnScreen(this);
		setVisible(true);
	}

	@Override
	public void chiudi() {
		dispose();

	}
}
